﻿using System;
using System.Linq;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Web.Modules.Shared.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Shared.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UserMessages : BaseUserControl<UserMessagesViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.IsAjaxRequest)
            {
                if (!Page.IsPostBack)
                {
                    GetAndUpdateUserMessageGrid();
                    UserMessageSenderControl1.SendButtonClicked += new EventHandler<AjaxEventArgs>(UserMessageSenderControl1_SendButtonClicked);
                }
            }
        }

        /// <summary>
        /// Gets the and update user message grid.
        /// </summary>
        private void GetAndUpdateUserMessageGrid()
        {
            int messagesCount = 0;
            UserMessagesViewModel.MessageDetails[] messages = this.ViewModel.GetUserMessages(RegistryInfo.UserID).ToArray();
            if (messages.Length > 0)
            {
                messagesCount = messages.Count();
            }
            storeMessages.DataSource = messages;
            storeMessages.DataBind();
            grvMessages.Title = this.ViewModel.GetUserName(RegistryInfo.UserID) + " - " + messagesCount.ToString();
        }

        /// <summary>
        /// Handles the Clicked event of the btnCompose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnCompose_Clicked(object sender, AjaxEventArgs e)
        {
            UserMessageSenderControl1.CleanControl();
            wndSendMessage.Show();
        }

        /// <summary>
        /// Handles the SendButtonClicked event of the UserMessageSenderControl1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void UserMessageSenderControl1_SendButtonClicked(object sender, AjaxEventArgs e)
        {
            wndSendMessage.Hide();
        }

        /// <summary>
        /// Handles the Command event of the GrvMessages control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void GrvMessages_Command(object sender, AjaxEventArgs e)
        {
            if (e.ExtraParams["CommandName"].Equals("Open"))
            {
                IUserMessage userMessage = this.ViewModel.GetMessageByID(e.ExtraParams["UserMessageID"].ToGuid());
                txtFrom.Text = userMessage.FromUserObj.DisplayName;
                txtDate.Text = userMessage.CreationDate.ToString();
                txtSubject.Text = userMessage.Subject;
                txtMessageBody.Text = userMessage.Message;
                wndOpenMessage.Show();
            }
            else
                if (e.ExtraParams["CommandName"].Equals("Delete"))
                {
                    this.ViewModel.DeleteUserMessage(e.ExtraParams["UserMessageID"].ToGuid());
                    GetAndUpdateUserMessageGrid();
                }
        }

        /// <summary>
        /// Handles the Refreshed event of the Paging control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Paging_Refreshed(object sender, EventArgs e)
        {
            GetAndUpdateUserMessageGrid();
        }
    }
}